﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.DuLieu;

namespace QldtSdh.BLL
{
    public partial class BizHocPhan
    {
        /// <summary>
        /// Lấy danh sách 
        /// </summary>
        /// <returns></returns>
        public static List<HocPhan> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.OrderBy(e => e.MaHocPhan).ToList();
            }
        }

        /// <summary>
        /// Lấy danh sách học phần theo đơn vị đào tạo
        /// </summary>
        /// <param name="idDonViDaoTao"></param>
        /// <returns></returns>
        internal static List<HocPhan> GetList(string guidIdDonViDaoTao)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.Where(e => e.MaDonViPhuTrachGUID == guidIdDonViDaoTao).OrderBy(e => e.MaHocPhan).ToList();
            }
        }

        public static HocPhan GetByID(string guidIdHocPhan, CEMS4UG_DHKHContext dbContext = null)
        {
            if (dbContext != null)
                return dbContext.HocPhanDbSet.Where(e => e.MaHocPhanGUID == guidIdHocPhan).FirstOrDefault();
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.Where(e => e.MaHocPhanGUID == guidIdHocPhan).FirstOrDefault();
            }
        }

        public static bool IsExistByID(string guidIdHocPhan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.Where(e => e.MaHocPhanGUID == guidIdHocPhan).FirstOrDefault() == null ? false : true;
            }
        }

        /// <summary>
        /// Kiểm tra Mã có tồn tại hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByCode(string maHocPhan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.Where(e => e.MaHocPhan == maHocPhan).FirstOrDefault() == null ? false : true;
            }
        }

        /// <summary>
        /// Kiểm tra tên có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(string tenHocPhan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.HocPhanDbSet.Where(e => e.TenHocPhan == tenHocPhan).FirstOrDefault() == null ? false : true;
            }
        }

        public static CODE_RESULT_RETURN Add(HocPhan obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.HocPhanDbSet.Where(e => e.MaHocPhan == obj.MaHocPhan).FirstOrDefault();
                if (_hp != null)
                    return CODE_RESULT_RETURN.MaTrung;
                //obj.MaHocPhanGUID = obj.MaHocPhan;
                db.HocPhanDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<HocPhan>(nhatKy, null, obj.MaHocPhanGUID, LOAIDOITUONG.HỌC_PHẦN, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<HocPhan>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(HocPhan obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.HocPhanDbSet.Where(e => e.MaHocPhan == obj.MaHocPhan && e.MaHocPhanGUID != obj.MaHocPhanGUID).FirstOrDefault();
                if (_hp != null)
                    return CODE_RESULT_RETURN.MaTrung;
                _hp = db.HocPhanDbSet.Where(e => e.MaHocPhanGUID == obj.MaHocPhanGUID).FirstOrDefault();
                if (_hp == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<HocPhan>(nhatKy, _hp, obj.MaHocPhanGUID, LOAIDOITUONG.HỌC_PHẦN, LOAITHAOTAC.Chỉnh_sửa);

                _hp.MaHocPhan = obj.MaHocPhan;
                _hp.TenHocPhan = obj.TenHocPhan;
                _hp.GhiChu = obj.GhiChu;
                _hp.HeSoDiemQTHT = obj.HeSoDiemQTHT;
                _hp.HinhThucHoc = obj.HinhThucHoc;
                _hp.HinhThucThi = obj.HinhThucThi;
                _hp.MaDonViPhuTrachGUID = obj.MaDonViPhuTrachGUID;
                _hp.SoTinChi = obj.SoTinChi;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<HocPhan>(nhatKy, _hp);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(string guidIdHocPhan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.HocPhanDbSet.Where(e => e.MaHocPhanGUID == guidIdHocPhan).FirstOrDefault();
                if (_hp == null)
                    return CODE_RESULT_RETURN.ThatBai;
                if (!(_hp.ListChuongTrinhDaoTao_HocPhan != null && _hp.ListChuongTrinhDaoTao_HocPhan.Count > 0) ||
                (_hp.HocVien_KetQuaHocTap != null && _hp.HocVien_KetQuaHocTap.Count > 0) ||
                (_hp.ListLopHocPhan != null && _hp.ListLopHocPhan.Count > 0))
                {
                    db.HocPhanDbSet.Remove(_hp);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<HocPhan>(nhatKy, _hp, _hp.MaHocPhanGUID, LOAIDOITUONG.HỌC_PHẦN, LOAITHAOTAC.Xóa);
                    BizNhatKyHoatDong.Add<HocPhan>(nhatKy, null);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }
}
